<template>
  <div class="comment-edit-container">
    <!-- 返回箭头 -->
    <div class="header">
      <el-button 
        icon="el-icon-arrow-left"
        @click="handleBack"
        class="back-button"
      ></el-button>
    </div>
    
    <!-- 表单区域 -->
    <el-form 
      :model="form"
      ref="formRef"
      label-width="120px"
      class="comment-form"
    >
      <el-form-item label="题目名称">
        <el-input v-model="form.questionName" disabled />
      </el-form-item>
      
      <el-form-item label="发布人名称">
        <el-input v-model="form.publisherName" disabled />
      </el-form-item>
      
      <el-form-item label="评论时间">
        <el-input v-model="form.commentTime" disabled />
      </el-form-item>
      
      <el-form-item label="评论内容" prop="commentContent">
        <el-input 
          v-model="form.commentContent"
          type="textarea"
          :rows="5"
          placeholder="请输入评论内容"
        />
      </el-form-item>
    </el-form>
    
    <!-- 底部按钮 -->
    <div class="button-group">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const formRef = ref(null)

// 表单数据
const form = reactive({
  id: '',
  questionName: '',
  publisherName: '',
  commentTime: '',
  commentContent: ''
})

// 模拟获取评论数据
const getCommentData = () => {
  // 在实际项目中，这里应该通过API获取数据
  const commentId = route.params.id
  // 模拟数据
  const mockComments = Array.from({ length: 50 }, (_, i) => ({
    id: i + 1,
    questionName: `题目${i + 1}`,
    publisherName: `用户${i % 10 + 1}`,
    commentContent: `这是关于题目${i + 1}的评论内容${i + 1}`,
    commentTime: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toLocaleString()
  }))
  
  // 查找匹配的评论
  const comment = mockComments.find(item => item.id === Number(commentId))
  if (comment) {
    Object.assign(form, comment)
  } else {
    ElMessage.error('未找到评论数据')
    router.push('/comment/index')
  }
}

// 返回上一页
const handleBack = () => {
  router.go(-1)
}

// 取消按钮
const handleCancel = () => {
  router.go(-1)
}

// 提交表单
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      // 在实际项目中，这里应该提交表单数据到API
      ElMessage.success('评论更新成功')
      router.push('/comment/index')
    }
  })
}

// 页面加载时获取数据
onMounted(() => {
  getCommentData()
})
</script>

<style scoped>
.comment-edit-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  margin-bottom: 30px;
}

.back-button {
  font-size: 18px;
}

.comment-form {
  margin-bottom: 40px;
}

.button-group {
  display: flex;
  justify-content: center;
  gap: 20px;
}
</style>